cmake_minimum_required(VERSION 2.6)
PROJECT(oce-win-bundle)

# Same path handling as OCE. I had to copy it here because of the structure of the main OCE file
if(WIN32)
	if(MSVC) # The compiler used is MSVC
		message(STATUS "Found MSVC compiler: ${MSVC} ${MSVC_VERSION}")
		set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin/)
	else()
		IF (MINGW)
			message(STATUS "Found MINGW compiler: ${MINGW}")
		endif(MINGW)
		set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/win${BIT}/bin/${CMAKE_BUILD_TYPE})
	endif()
	set(EXECUTABLE_OUTPUT_PATH ${LIBRARY_OUTPUT_PATH})
else(WIN32)
	set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/${PLATFORM}/${CMAKE_SYSTEM_PROCESSOR}-${CMAKE_BUILD_TYPE}-${BIT})
endif(WIN32)
message("oce_output_path: ${LIBRARY_OUTPUT_PATH}")

# Policy CMP0043 introduced in cmake version 3.0 IGNORES the use of COMPILE_DEFINITIONS suffixed variables, e.g. COMPILE_DEFINITIONS_DEBUG
# Set to OLD behavior until minimum cmake version >= 2.8.10 (version that COMPILE_DEFINITIONS can be set by generator expressions instead)
if (POLICY CMP0043)
	cmake_policy(SET CMP0043 OLD)
endif (POLICY CMP0043)

# Fix issues with ZLib
REMOVE_DEFINITIONS("-DHAVE_CONFIG_H")

# Standard debug postfix
IF(MSVC)
    SET(CMAKE_DEBUG_POSTFIX "d")
ENDIF(MSVC)

# Library paths
SET(FT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/freetype)
SET(GL2PS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/gl2ps)
SET(FREEIMAGE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/FreeImage)
SET(LIBPNG_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/FreeImage/Source/LibPNG/)
SET(ZLIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/FreeImage/Source/zlib/)

# Compilation options based on OCE settings
SET(BUNDLE_SHARED_LIBRARIES ${OCE_BUILD_SHARED_LIB})

IF(OCE_OCAF OR OCE_VISUALISATION)
  SET(BUNDLE_BUILD_FREETYPE ON)
ELSE()
  SET(BUNDLE_BUILD_FREETYPE OFF)
ENDIF()

# Still not supported
SET(BUNDLE_BUILD_VTK OFF)
SET(BUNDLE_BUILD_GL2PS ${OCE_WITH_GL2PS})
SET(BUNDLE_BUILD_FREEIMAGE ${OCE_WITH_FREEIMAGE})

IF(${OCE_MULTITHREAD_LIBRARY} STREQUAL "TBB")
  SET(BUNDLE_BUILD_TBB ON)
ENDIF()

IF(BUNDLE_SHARED_LIBRARIES)
  SET(BUNDLE_LIB_MODIFIER "SHARED")
ELSE()
  SET(BUNDLE_LIB_MODIFIER "")
ENDIF()

IF(MINGW)
  IF(BUNDLE_SHARED_LIBRARIES)
	  SET(BUNDLE_LIB_EXT "dll.a")
  ELSE()
      SET(BUNDLE_LIB_EXT "a")
  ENDIF()
ENDIF()


SET( OCE_WIN_BUNDLE_INSTALL_DIR_BITS ${OCE_WIN_BUNDLE_INSTALL_DIR}/Win${BIT}/)
SET( OCE_BUNDLE_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR} )

# Adds freetype and related variables
IF(BUNDLE_BUILD_FREETYPE)
  ADD_SUBDIRECTORY(libs/freetype)

  set(FREETYPE_INCLUDE_DIR_freetype2 ${OCE_BUNDLE_ROOT_PATH}/libs/freetype CACHE PATH "" FORCE)
	set(FREETYPE_INCLUDE_DIR_ft2build ${OCE_BUNDLE_ROOT_PATH}/libs/freetype/include CACHE PATH "" FORCE)
	set(FREETYPE_INCLUDE_DIRS "${FREETYPE_INCLUDE_DIR_freetype2}" "${FREETYPE_INCLUDE_DIR_ft2build}" PARENT_SCOPE)

	set(FREETYPE_BIN_DIR ${LIBRARY_OUTPUT_PATH})
  
	if(MINGW)
		# The archive output directory is set for MinGW, since the new methodology (see statement above)
		# seems not to work with the MinGw generator
		
		set(FREETYPE_LIBRARY ${FREETYPE_BIN_DIR}/libfreetype.${BUNDLE_LIB_EXT} CACHE FILEPATH "" FORCE)
		set(FREETYPE_LIBRARIES ${FREETYPE_BIN_DIR}/libfreetype.${BUNDLE_LIB_EXT} CACHE FILEPATH "" FORCE)
	else(MINGW)
		set(FREETYPE_LIBRARY ${FREETYPE_BIN_DIR}/$<CONFIG>/freetype.lib CACHE FILEPATH "" FORCE)
		set(FREETYPE_LIBRARIES ${FREETYPE_BIN_DIR}/$<CONFIG>/freetype.lib CACHE FILEPATH "" FORCE)
		set(FREETYPE_LIBRARY_DEBUG ${FREETYPE_BIN_DIR}/$<CONFIG>/freetyped.lib CACHE FILEPATH "" FORCE)
	endif(MINGW)
	
	if (USE_SOLUTION_FOLDERS)
     set_target_properties(freetype PROPERTIES FOLDER oce-win-bundle )
	endif(USE_SOLUTION_FOLDERS)
ENDIF()

IF(BUNDLE_BUILD_GL2PS)
  ADD_SUBDIRECTORY(gl2ps.cmake)
  
  set(GL2PS_BIN_DIR ${LIBRARY_OUTPUT_PATH})
   	
	if(MINGW)
	  	set(GL2PS_LIBRARY ${GL2PS_BIN_DIR}/libgl2ps.${BUNDLE_LIB_EXT} CACHE FILEPATH "" FORCE)
			set(GL2PS_LIBRARY_DEBUG ${GL2PS_BIN_DIR}/libgl2ps.${BUNDLE_LIB_EXT} CACHE FILEPATH "" FORCE)
	else(MINGW)
			set(GL2PS_LIBRARY ${GL2PS_BIN_DIR}/$<CONFIG>/gl2ps.lib CACHE FILEPATH "" FORCE)
			set(GL2PS_LIBRARY_DEBUG ${GL2PS_BIN_DIR}/$<CONFIG>/gl2psd.lib CACHE FILEPATH "" FORCE)
   endif(MINGW)
		
	set(GL2PS_INCLUDE_DIR ${OCE_BUNDLE_ROOT_PATH}/libs/gl2ps CACHE PATH "" FORCE)
		
  if (USE_SOLUTION_FOLDERS)
     set_target_properties(gl2ps PROPERTIES FOLDER oce-win-bundle )
	endif(USE_SOLUTION_FOLDERS)
ENDIF()

IF(BUNDLE_BUILD_FREEIMAGE)
  ADD_SUBDIRECTORY(FreeImage.cmake)
  
  set(FREEIMAGE_BIN_DIR ${LIBRARY_OUTPUT_PATH})

  if(MINGW)
    set(FREEIMAGE_LIBRARY ${FREEIMAGE_BIN_DIR}/libFreeImage.${BUNDLE_LIB_EXT} CACHE FILEPATH "" FORCE)
    set(FREEIMAGE_LIBRARY_DEBUG ${FREEIMAGE_BIN_DIR}/libFreeImage.${BUNDLE_LIB_EXT} CACHE FILEPATH "" FORCE)
  else(MINGW)
    set(FREEIMAGE_LIBRARY ${FREEIMAGE_BIN_DIR}/$<CONFIG>/FreeImage.lib CACHE FILEPATH "" FORCE)
    set(FREEIMAGE_LIBRARY_DEBUG ${FREEIMAGE_BIN_DIR}/$<CONFIG>/FreeImaged.lib CACHE FILEPATH "" FORCE)
  endif(MINGW)

   if(MINGW)
    set(FREEIMAGEPLUS_LIBRARY ${FREEIMAGE_BIN_DIR}/libFreeImagePlus.${BUNDLE_LIB_EXT} CACHE FILEPATH "" FORCE)
    set(FREEIMAGEPLUS_LIBRARY_DEBUG ${FREEIMAGE_BIN_DIR}/libFreeImagePlus.${BUNDLE_LIB_EXT} CACHE FILEPATH "" FORCE)
  else(MINGW)
    set(FREEIMAGEPLUS_LIBRARY ${FREEIMAGE_BIN_DIR}/$<CONFIG>/FreeImagePlus.lib CACHE FILEPATH "" FORCE)
    set(FREEIMAGEPLUS_LIBRARY_DEBUG ${FREEIMAGE_BIN_DIR}/$<CONFIG>/FreeImagePlusd.lib CACHE FILEPATH "" FORCE)
  endif(MINGW)
  
  set(FREEIMAGE_INCLUDE_DIR ${OCE_BUNDLE_ROOT_PATH}/libs/FreeImage/Source ${OCE_BUNDLE_ROOT_PATH}/libs/FreeImage/Wrapper/FreeImagePlus CACHE PATH "" FORCE)
			
  if (USE_SOLUTION_FOLDERS)
     set_target_properties(FreeImage PROPERTIES FOLDER oce-win-bundle )
     set_target_properties(FreeImagePlus PROPERTIES FOLDER oce-win-bundle )
	endif(USE_SOLUTION_FOLDERS)
ENDIF()

IF(BUNDLE_BUILD_TBB)
  ADD_SUBDIRECTORY(libs/TBB)
  
  set(TBB_INCLUDE_DIR ${OCE_BUNDLE_ROOT_PATH}/libs/TBB/include CACHE PATH "" FORCE)
  set(TBB_BIN_DIR ${LIBRARY_OUTPUT_PATH})
  set(TBB_LIBRARY ${TBB_BIN_DIR}/$<CONFIG>/tbb.lib CACHE FILEPATH "" FORCE)
  set(TBB_LIBRARY_DEBUG ${TBB_BIN_DIR}/$<CONFIG>/tbb_debug.lib CACHE FILEPATH "" FORCE)
  set(TBB_MALLOC_LIBRARY ${TBB_BIN_DIR}/$<CONFIG>/tbbmalloc.lib CACHE FILEPATH "" FORCE)
  set(TBB_MALLOC_LIBRARY_DEBUG ${TBB_BIN_DIR}/$<CONFIG>/tbbmalloc_debug.lib CACHE FILEPATH "" FORCE)
  
  if (USE_SOLUTION_FOLDERS)
     if(NOT OCE_BUILD_SHARED_LIB)
        set(tbb_prefix "_static")
     endif()
     set_target_properties(tbb${tbb_prefix} PROPERTIES FOLDER oce-win-bundle )
     set_target_properties(tbbmalloc${tbb_prefix} PROPERTIES FOLDER oce-win-bundle )
     set_target_properties(tbb_def_files PROPERTIES FOLDER oce-win-bundle )
	endif(USE_SOLUTION_FOLDERS)
ENDIF()

## STILL NOT SUPPORTED!
IF(BUNDLE_BUILD_VTK)
	 ADD_SUBDIRECTORY(VTK.cmake)
ENDIF()